package com.centaline.androidsalesblog.base;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.iml.LoanCalculateCallback;
import com.centaline.androidsalesblog.model.databaseobject.SearchDataDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 房贷计算基类
 * Created by guilin on 16/3/1.
 */
public abstract class BaseLoanCalFragment extends BaseFragment {

    protected final String[] ratios = new String[]{"2成", "2.5成", "3成", "4成", "5成", "6成", "7成", "8成", "9成"};
    protected final double[] ratiosDouble = new double[]{2, 2.5, 3, 4, 5, 6, 7, 8, 9};
    protected final String[] loanTypes = new String[]{"等额本息", "等额本金"};

    protected ArrayList<SearchDataDo> bizRateArrayList = new ArrayList<>();//商业利率
    protected ArrayList<SearchDataDo> fundRateArrayList = new ArrayList<>();//公积金利率

    protected AlertDialog ratioDialog;//首付比例
    protected AlertDialog loanTypeDialog;//贷款方式

    protected int mode_calc_type = 1;//计算方式,默认按揭
    protected double ratio;//按揭比例
    protected int loanType;//贷款方式
    protected double lastRate;//最终计算出的利率

    protected LoanCalculateCallback loanCalculateCallback;

    public void setLoanCalculateCallback(LoanCalculateCallback loanCalculateCallback) {
        this.loanCalculateCallback = loanCalculateCallback;
    }

    @Override
    protected void initView() {
        initData();
    }

    /**
     * 初始化的数据
     */
    protected abstract void initData();

    /**
     * 商业贷款利率初始化
     */
    protected void initBizRate() {
    }

    /**
     * 公积金贷款利率初始化
     */
    protected void initFundRate() {
    }

    /**
     * 设置计算点击
     */
    protected void setCalcClick(View view) {
        RxView.clicks(view)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(this.<Void>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startCalculate();
                    }
                });
    }

    /**
     * 开始计算
     */
    protected abstract void startCalculate();

    /**
     * 首付比例
     */
    protected void showRatioDialog() {
        if (ratioDialog == null) {
            ratioDialog = new AlertDialog.Builder(getContext())
                    .setTitle(R.string.calc_mortgage_ratio)
                    .setSingleChoiceItems(ratios, 2, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ratio = ratiosDouble[which];
                            ratioItemClick(ratios[which]);
                            ratioDialog.dismiss();
                        }
                    })
                    .create();
        }
        ratioDialog.show();
    }

    /**
     * 首付比例
     */
    protected void ratioItemClick(String text) {

    }

    /**
     * 贷款方式
     */
    protected void showLoanTypeDialog() {
        if (loanTypeDialog == null) {
            loanTypeDialog = new AlertDialog.Builder(getContext())
                    .setTitle("贷款方式")
                    .setSingleChoiceItems(loanTypes, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            loanType = which;
                            loanTypeItemClick(loanTypes[which]);
                            loanTypeDialog.dismiss();
                        }
                    })
                    .create();
        }
        loanTypeDialog.show();
    }

    /**
     * 贷款方式
     */
    protected void loanTypeItemClick(String text) {

    }

    /**
     * 商贷利率
     */
    protected void bizRate() {
        RxDataBase
                .getSearchData("BizRate")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<SearchDataDo>>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<ArrayList<SearchDataDo>>() {
                    @Override
                    public void call(ArrayList<SearchDataDo> searchDataDos) {
                        bizRateArrayList.clear();
                        bizRateArrayList.addAll(searchDataDos);
                        initBizRate();
                    }
                });
    }

    /**
     * 公积金利率
     */
    protected void fundRate() {
        RxDataBase
                .getSearchData("FundRate")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<SearchDataDo>>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<ArrayList<SearchDataDo>>() {
                    @Override
                    public void call(ArrayList<SearchDataDo> searchDataDos) {
                        fundRateArrayList.clear();
                        fundRateArrayList.addAll(searchDataDos);
                        initFundRate();
                    }
                });
    }
}
